package com.thauanLopes.smagasmaga;

import java.util.ArrayList;
import java.util.List;

import android.R.menu;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.thauanLopes.bacterium.Menu;
import com.game.ThauanFramework.BitmapStorage;
import com.game.ThauanFramework.GameObject;
import com.game.ThauanFramework.SaveManager;
import com.game.ThauanFramework.TimeManager;
import com.game.ThauanFramework.Util;

public class SplashView extends View implements Runnable {

	private float splashTime = 3;
	private final String TAG = "Splash View";

	private Thread mainThread;
	private int interval = 10;

	private boolean BitmapStorageLauched = false;
	private boolean running = true;

	private Activity ac;

	private List<GameObject> gameObjects = new ArrayList<GameObject>();

	public SplashView(Context context, Activity ac) {
		super(context);

		this.ac = ac;
		running = true;
		this.setFocusable(true);

		Start();
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		if (Util.getCanvasHeight() == 0) {
			Util.setCanvasHeight(h);
			Util.setCanvasWidth(w);
		}

		this.setBackgroundResource(R.drawable.wanted_logo);

		mainThread = new Thread(this);
		mainThread.start();

		Log.i(TAG, "on size Changed");
	}

	public boolean onTouchEvent(MotionEvent event) {
		return false;
	}

	// ------------------------------------------------------------- START
	public void Start() {

	}

	// ------------------------------------------------------------- UPDATE

	float cronometro = 0;

	public void Update() {
		if (cronometro < this.splashTime) {
			cronometro += TimeManager.getInstance().getDeltaTime();

			Log.i(TAG, String.valueOf(cronometro));

			if (cronometro >= 2 && !BitmapStorageLauched) {
				BitmapStorageLauched = true;
				BitmapStorage.getInstance();
			}
		} else {

			// Go to the menu Activity
			Util.ChangeActivity(Menu.class);

			this.SetAlive(false);
			Log.i(TAG, "Fim de Splash");

		}

	}

	// ------------------------------------------------------------- DRAW
	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
		for (GameObject go : gameObjects) {
			go.Draw(canvas);
		}

	}

	public void SetAlive(boolean bool) {
		this.running = bool;
	}

	@Override
	public void run() {

		// Starts the loop
		while (running) {
			try {
				Thread.sleep(interval);
			} catch (InterruptedException e) {
				running = false;
				Log.i(TAG, "Erro no processo de Thread", e);
			}

			Update();
			postInvalidate();

		}
		this.ac.finish();
	}
}
